package uit.shoeshop.model;

import uit.shoeshop.dto.ProductDTO;
import uit.shoeshop.dao.ProductDAO;
import uit.shoeshop.dao.CategoryDAO;
import uit.shoeshop.dto.CategoryDTO;
import uit.shoeshop.dao.BillDAO;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
public class ProductModel {
	public ProductModel(){
		
	}
	
	public ArrayList<ProductDTO> getAllProduct(int page) throws SQLException{
		ArrayList<ProductDTO> product = new ArrayList<ProductDTO>();

		ProductDAO tblProduct = new ProductDAO();
		/*
		 * Lay tat ca cac featured product
		 */
		product = tblProduct.getAllProduct(page);
		
		tblProduct = null;
		return product;
	}
	
	public ProductDTO getProductById(int id) throws SQLException{
		ProductDTO product;

		ProductDAO tblProduct = new ProductDAO();
		/*
		 * Lay tat ca cac featured product
		 */
		product = tblProduct.getProductDetailRaw(id);
		
		tblProduct = null;
		return product;		
	}
	
	public HashMap<Integer, String> getAllCategory() throws SQLException{
		HashMap<Integer,String> results = new HashMap<Integer, String>();
		ArrayList<CategoryDTO> categories;
		CategoryDAO tblCategory = new CategoryDAO();
		
		categories = tblCategory.getAllCategory();
		
		if(categories.size() > 0){
			for (CategoryDTO cate : categories) {
				results.put(cate.getCategory_id(), cate.getTitle());
			}
		}
		return results;
	}
	
	public int delProduct(int pId) throws SQLException{
		int result = 1;
		BillDAO tblBill = new BillDAO();
		if(tblBill.checkBillDetail(pId) > 0){
			result = 2;
		}else{
			ProductDAO tblproduct = new ProductDAO();
			result = tblproduct.delProduct(pId);
		}
		return result;
	}
	
	public int activeProduct(int pId,int enable) throws SQLException{
		int result = 1;

		ProductDAO tblproduct = new ProductDAO();
		result = tblproduct.updateActive(pId, enable);

		return result;
	}
	
	public int updateProduct(int pId, String title, String desc, int price,
			int stock, int cate, int enabled, String image0, String image1,
			String image2, String image3) throws SQLException {
		
		int result = 1;
		ProductDAO tblproduct = new ProductDAO();
		result = tblproduct.updateProduct(pId, title, desc, price, stock, cate, enabled, image0, image1, image2, image3);
		return result;
	}

	public int insertProduct(String title, String desc, int price,
			int stock, int cate, int enabled, String image0, String image1,
			String image2, String image3) throws SQLException {
		
		int result = 1;
		ProductDAO tblproduct = new ProductDAO();
		result = tblproduct.insertProduct(title, desc, price, stock, cate, enabled, image0, image1, image2, image3);
		return result;
	}	
	
	public int countTotalProduct() throws SQLException{
		int result = 0;
		ProductDAO tblproduct = new ProductDAO();
		result = tblproduct.countAllProduct();
		return result;
	}
	
	public ArrayList<CategoryDTO> getAllParentCategory() throws SQLException{
		ArrayList<CategoryDTO> category = new ArrayList<CategoryDTO>();

		CategoryDAO tblCategory = new CategoryDAO();
		
		/*
		 * Lay tat ca cac category cha
		 */
		category = tblCategory.getCategoryRaw(0);
		
		tblCategory = null;
		return category;
	}

	public HashMap<Integer, ArrayList<CategoryDTO>> getAllChildCategory(ArrayList<CategoryDTO> parentCate) throws SQLException{
		HashMap<Integer, ArrayList<CategoryDTO>> category = new HashMap<Integer, ArrayList<CategoryDTO>>();
		CategoryDAO tblCategory = new CategoryDAO();		
		int cateId = 0 ;
		ArrayList<CategoryDTO> temp;
		
		for (CategoryDTO cate : parentCate) {
			cateId = cate.getCategory_id();
			temp = tblCategory.getCategoryRaw(cateId);
			if(temp.size() > 0){
				category.put(cateId, temp);
			}
		}
		
		temp = null;
		tblCategory = null;
		return category;
	}

}
